﻿using System;
using System.IO;

namespace CProcess.Services
{
    class BackupPlan
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private const char Separator = ';';

        /// <summary>
        /// Start copying file(s) to a specific destination. Source must be absolute path, like c:\temp\file.txt
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <example>source1;source2</example>
        public static void Run(string source, string destination)
        {
            try
            {
                if (source.Contains(Separator.ToString()))
                {
                    var files = source.Split(Separator);
                    foreach (var file in files)
                    {
                        Copy(file, destination);
                    }
                }   
                else
                {
                    Copy(source, destination);
                }
            }
            catch(Exception e)
            {                
                log.Fatal(e.Message);
            }
        }

        private static void Copy(string source, string destination)
        {            
            if (File.Exists(source))
            {
                var fileName = Path.GetFileName(source);

                File.Copy(source, String.Format("{0}\\{1}", destination, fileName), true);                
                log.Debug(String.Format("Copying {0} to {1} has been finished!", source, destination));
            }
            else
            {
                var message = String.Format("Source {0} cannot be found. Please check that the file exists and try again!",
                                            source);
                log.Error(message);
            }
        }
    }
}
